package renshi.action;

import java.sql.Connection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import renshi.base.BaseAction;
import renshi.dao.AdminDAO;
import renshi.model.Apply;
import renshi.model.Student;
import renshi.model.Tclass;
import renshi.model.Teacher;
import renshi.utils.DBUtils;

public class AdminAction extends BaseAction{
	
	private String num;
	private String name;
	private String cname;
	private String pwd;
	private int aid;
	public int getAid() {
		return aid;
	}
	public void setAid(int aid) {
		this.aid = aid;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public AdminDAO getAdminDAO() {
		return adminDAO;
	}
	public void setAdminDAO(AdminDAO adminDAO) {
		this.adminDAO = adminDAO;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	private static final long serialVersionUID = 1L;
	AdminDAO adminDAO;
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpSession session = request.getSession();
	//----------------�鿴���еĽ�ʦ
	public String teacherinf()throws Exception{
		openCon();
		adminDAO = new AdminDAO(con);
		List<Teacher> teacherlist =adminDAO.teacherinf();
		closeCon();
		if(teacherlist!=null){
        	request.setAttribute("teacherlist", teacherlist);
        	request.getAttribute("teacherlist");
        	System.out.println("AdminAction�е�teacherlist��==null");
        	return "teacherinf";
        	
        }
    	else
        	return "loginFail";
	}
	//-------------------�鿴���е�ѧ��
	public String studentinf()throws Exception{
		openCon();
		adminDAO = new AdminDAO(con);
		List<Student> studentlist =adminDAO.studentinf();
		closeCon();
		if(studentlist!=null){
        	request.setAttribute("studentlist", studentlist);
        	request.getAttribute("studentlist");
        	System.out.println("AdminAction�е�studentlist��==null");
        	return "studentinf";
        	
        }
    	else
        	return "loginFail";
	}
	//--------------�鿴����ѧ�����ϸ��Ϣ
	public String sdetail()throws Exception{
		
		Connection con = DBUtils.getConnection();
	    con.setAutoCommit(false);
		adminDAO = new AdminDAO(con);
		 String id = request.getParameter("id");
		int numid=0;
	    if(id!=null){
	       numid=Integer.parseInt(id);
	    }
	    System.out.println("dddddddddddddddddd"+numid);
		Student student =adminDAO.sdetail(numid);
		closeCon();
		if(student!=null){
        	request.setAttribute("student", student);
        	Tclass tclass=adminDAO.steacher(student.getCname());
        	request.setAttribute("tclass", tclass);
        	System.out.println("ssssssssssssss"+student.getCname());
        	System.out.println("ssssssssssssss"+tclass.getTname());
        	return "sdetail";
        	
        }
    	else
        	return "loginFail";
	}
	//--------------�鿴������ʦ����ϸ��Ϣ
	public String tdetail()throws Exception{
		
		Connection con = DBUtils.getConnection();
	    con.setAutoCommit(false);
		adminDAO = new AdminDAO(con);
		 String tid = request.getParameter("tid");
		int numid=0;
	    if(tid!=null){
	       numid=Integer.parseInt(tid);
	    }
	    System.out.println("dddddddddddddddddd"+numid);
		Teacher teacher =adminDAO.tdetail(numid);
		closeCon();
		if(teacher!=null){
        	request.setAttribute("teacher", teacher);
        	Tclass tclass=adminDAO.tclass(teacher.getTurename());
        	request.setAttribute("tclass", tclass);
        	System.out.println("ssssssssssssss"+teacher.getTurename());
        	System.out.println("ttttttttttttttt"+tclass.getCname());
        	return "tdetail";
        	
        }
    	else
        	return "loginFail";
	}
	//-------------------�鿴���е���ٵ�
	public String allapply()throws Exception{
		openCon();
		adminDAO = new AdminDAO(con);
		List<Apply> applylist =adminDAO.allapply();
		closeCon();
		if(applylist!=null){
        	request.setAttribute("applylist", applylist);
        	request.getAttribute("applylist");
        	System.out.println("AdminAction�е�studentlist��==null");
        	return "allapply";
        	
        }
    	else
        	return "loginFail";
	}
	
	//-------------------�鿴���������ٵ�
	public String applyed()throws Exception{
		openCon();
		adminDAO = new AdminDAO(con);
		List<Apply> applylist =adminDAO.applyed();
		closeCon();
		if(applylist!=null){
        	request.setAttribute("applylist", applylist);
        	request.getAttribute("applylist");
        	System.out.println("AdminAction�е�studentlist��==null");
        	return "applyed";
        	
        }
    	else
        	return "loginFail";
	}
	//-------------------�鿴����������3�����ٵ�
	public String longApply()throws Exception{
		openCon();
		adminDAO = new AdminDAO(con);
		List<Apply> applylist =adminDAO.longApply();
		closeCon();
		if(applylist!=null){
        	request.setAttribute("applylist", applylist);
        	request.getAttribute("applylist");
        	System.out.println("AdminAction�е�studentlist��==null");
        	return "longApply";
        	
        }
    	else
        	return "loginFail";
	}
	//-------------------������ٵ�
	public String alongApply()throws Exception{
		openCon();
		adminDAO = new AdminDAO(con);
		List<Apply> applylist =adminDAO.alongApply();
		closeCon();
		if(applylist!=null){
        	request.setAttribute("applylist", applylist);
        	request.getAttribute("applylist");
        	System.out.println("AdminAction�е�studentlist��==null");
        	return "alongApply";
        	
        }
    	else
        	return "loginFail";
	}
	//-----------------�̼���ٵ�
	public String shortApply()throws Exception{
		openCon();
		adminDAO = new AdminDAO(con);
		List<Apply> applylist =adminDAO.shortapply();
		closeCon();
		if(applylist!=null){
        	request.setAttribute("applylist", applylist);
        	request.getAttribute("applylist");
        	System.out.println("AdminAction�е�studentlist��==null");
        	return "shortApply";
        	
        }
    	else
        	return "loginFail";
	}
	//-------------���ѧ��
public String addStudent()throws Exception{
		
		Connection con = DBUtils.getConnection();
        con.setAutoCommit(false);
		adminDAO = new AdminDAO(con);
		boolean flag = adminDAO.addStudent(num,name,cname,pwd);
        con.commit();
        if(flag==true){
        	return "addStuSuccess";
        }
    	else
        	return "loginFail";
	}
	public String delete(){
		return "deleteSuccess";
	}

	public String query(){
		return "querySuccess";
		
	}
	
	public String update(){
		return "updateSuccess";
	}
	//-------------ɾ����ٵ�
	public String deleteA()throws Exception{
			
			Connection con = DBUtils.getConnection();
	        con.setAutoCommit(false);
	        String applyid = request.getParameter("applyid");
	        int numid=0;
	        if(applyid!=null){
	           numid=Integer.parseInt(applyid);
	        }
			adminDAO = new AdminDAO(con);
			boolean flag = adminDAO.deleteA(numid);
	        con.commit();
	        if(flag==true){
	        	return "deleteA";
	        }
	    	else
	        	return "loginFail";
		}
	//-------------ɾ����ʦ
	public String deleteT()throws Exception{
			
			Connection con = DBUtils.getConnection();
	        con.setAutoCommit(false);
	        String tid = request.getParameter("tid");
	        int numid=0;
	        if(tid!=null){
	           numid=Integer.parseInt(tid);
	        }
			adminDAO = new AdminDAO(con);
			boolean flag = adminDAO.deleteT(numid);
	        con.commit();
	        if(flag==true){
	        	return "deleteA";
	        }
	    	else
	        	return "loginFail";
		}
	//-------------ɾ��ѧ��
	public String deleteS()throws Exception{
			
			Connection con = DBUtils.getConnection();
	        con.setAutoCommit(false);
	        String id = request.getParameter("id");
	        int numid=0;
	        if(id!=null){
	           numid=Integer.parseInt(id);
	        }
			adminDAO = new AdminDAO(con);
			boolean flag = adminDAO.deleteS(numid);
	        con.commit();
	        if(flag==true){
	        	return "deleteA";
	        }
	    	else
	        	return "loginFail";
		}
}
